package org.sunshine.mamadu.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.sunshine.mamadu.Advertisement;

import java.util.ArrayList;
import java.util.List;

public class TableShell {

    private Shell internalShell;

    private Table table;

    private static final String[] columnNames = {
            "content",
            "name",
            "phone",
            "email",
            "date"};

    private static final int[] columsSizes = {
            200,
            100,
            100,
            100,
            100};

    public TableShell(final Display display) {
        internalShell = new Shell(display, SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.RESIZE);
        internalShell.setImage(MainFrame.getIcon());
        internalShell.setSize(400, 400);
        internalShell.setLayout(new FillLayout());
        table = new Table(internalShell, SWT.MULTI | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.addSelectionListener(new SelectionAdapter() {
            public void widgetDefaultSelected(SelectionEvent e) {
                TableItem[] tableItems = table.getSelection();
                final Advertisement ad = (Advertisement) tableItems[0].getData();
                display.asyncExec(new Runnable() {
                    public void run() {
                        new AdvertisementDetailWindow(display, ad);
                    }
                });

            }
        });

        internalShell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
                e.doit = false;
                hide();
            }

            public void shellIconified(ShellEvent e) {
                hide();
            }
        });


        for (int i = 0; i < columnNames.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(columnNames[i]);
            column.setWidth(columsSizes[i]);
            final int columnIndex = i;
            column.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
//                    sort(columnIndex);
                }
            });
        }


        table.setLinesVisible(true);

        internalShell.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 400);


    }

    public void show(List<Advertisement> list) {
        table.removeAll();
        for (Advertisement advertisement : list) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[]{
                    advertisement.getContent(),
                    advertisement.getName(),
                    advertisement.getPhone(),
                    advertisement.getEmail(),
                    advertisement.getDate(),
            });
            item.setData(advertisement);
        }
        internalShell.open();
        internalShell.setMinimized(false);
    }

    public boolean isVisible() {
        return internalShell.isVisible();
    }

    public void hide() {
        internalShell.setMinimized(true);
        internalShell.setVisible(false);
    }


    private boolean onClose() {
        TableItem[] items = table.getItems();
        for (int i = 0; i < items.length; i++) {
            items[i].dispose();
        }
        return true;
    }


    public static void main(String[] args) {

        ArrayList<Advertisement> list = new ArrayList<Advertisement>();
        Advertisement ad1 = new Advertisement();
        ad1.setName("Kolyan");
        ad1.setEmail("Kolyan@mail.com");
        ad1.setDate("20/50/2007");
        ad1.setPhone("234234234");
        ad1.setContent("сдаю без комиссии 1 комн квартиру 7 мин.пишком от метро Варшавская. " +
                "Вся мебель и бытовая новые стоимость 22500р в месяц +свет телефон и интернет." +
                "Посредникам строго не звонить.");

        Advertisement ad2 = new Advertisement();
        ad2.setName("Kolyan");
        ad2.setEmail("Kolyan@mail.com");
        ad2.setDate("20/50/2007");
        ad2.setPhone("234234234");
        ad2.setContent("сдаю без комиссии 1 комн квартиру 7 мин.пишком от метро Варшавская. " +
                "Вся мебель и бытовая новые стоимость 22500р в месяц +свет телефон и интернет." +
                "Посредникам строго не звонить.");
        list.add(ad1);
        list.add(ad2);

        Display display = new Display();
        TableShell tableShell = new TableShell(display);
        tableShell.show(list);

        while (!tableShell.internalShell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
        System.exit(0);


    }
}
